<template>
  <div v-show="bottomVisible" class="scroll-to-bottom" @click="scrollToBottom">
    <i class="el-icon-caret-bottom"></i> <!-- 使用向下箭头图标 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      bottomVisible: false, // 控制滚动到底部按钮的显示/隐藏
      lastScrollTop: 0, // 用于存储上一次滚动的位置
    };
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll() {
      // 获取当前滚动位置
      let currentScrollTop = window.pageYOffset || document.documentElement.scrollTop;

      // 判断滚动方向：如果当前滚动位置大于上次记录的位置，则为向下滚动
      if (currentScrollTop > this.lastScrollTop) {
        this.bottomVisible = true;
      } else {
        this.bottomVisible = false;
      }

      // 更新最后的滚动位置
      this.lastScrollTop = currentScrollTop <= 0 ? 0 : currentScrollTop; // For Mobile or negative scrolling
    },
    scrollToBottom() {
      // 直接滚动到页面最底部
      window.scrollTo({
        top: document.documentElement.scrollHeight,
        behavior: 'instant' // 平滑滚动
      });
    }
  }
};
</script>

<style scoped>
.scroll-to-bottom {
  position: fixed;
  bottom: 20px;
  left: 660px;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background-color: #65a9f1;
  color: white;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.12);
  z-index: 1000;
}

.scroll-to-bottom:hover {
  background-color: #0566d1;
}
</style>
